package com.liaoban.app.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.adapter.RuleAdapter;
import com.liaoban.app.adapter.ShareMoneyAdapter;
import com.liaoban.app.api.type.Rule;
import com.liaoban.app.api.type.event.ShareTixianSucEvent;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseShareActivity;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.ui.common.EmptyView;
import com.liaoban.app.util.CommonUtils;
import com.liaoban.app.widget.MyAlertDialog;
import com.liaoban.app.widget.MyGridLayoutManager;
import com.netease.nim.avchatkit.event.ChongZhiEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/12 0012.
 * 分享赚钱
 */
public class ShareMoneyActivity extends BaseShareActivity {
    @BindView(R.id.root)
    View root;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_keti_money)
    TextView tvKeTiMoney;
    @BindView(R.id.tv_leiji_money)
    TextView tvLeiJiMoney;
    @BindView(R.id.tv_user_num)
    TextView tvUserNum;
    @BindView(R.id.rv_rule)
    RecyclerView rvRule;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private RuleAdapter mRuleAdapter;
    private ShareMoneyAdapter mAdapter;
    //private int mPageIndex = 1;

    @Override
    protected String getNewTitle() {
        return getString(R.string.share_money);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_share_money;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /*tvRight.setText("邀请码");
        tvRight.setVisibility(View.VISIBLE);*/
        setNeedOnBus(true);
        initRuleRecyclerView();
        initRecyclerView();
        showRunningDialog();
        loadData();
    }

    private void initRuleRecyclerView() {
        List<Rule> rules = new ArrayList<>();
        Rule rule = new Rule();
        rule.setTitle1("奖励一：");
        rule.setTitle2("好友充值永久奖励3%");
        rule.setPrice("3元/人");
        rule.setContent("即你邀请的用户每次充值，你都将获得该充值金额3%的提成（不包含活动期间平台赠送金额）");
        rules.add(rule);

        Rule rule2 = new Rule();
        rule2.setTitle1("奖励二：");
        rule2.setTitle2("好友收益永久奖励3%");
        rule2.setPrice("5%");
        rule2.setContent("即你邀请的用户每次收入，你都将获得该收入金额3%的提成（不包含活动期间平台赠送金额）");
        rules.add(rule2);

        MyGridLayoutManager manager = new MyGridLayoutManager(this, 1);
        rvRule.setLayoutManager(manager);
        mRuleAdapter = new RuleAdapter(rules);
        rvRule.setAdapter(mRuleAdapter);
    }

    private void initRecyclerView() {
        MyGridLayoutManager manager = new MyGridLayoutManager(this, 1);
        rvList.setLayoutManager(manager);
        mAdapter = new ShareMoneyAdapter(null);
        rvList.setAdapter(mAdapter);
        /*mAdapter.setOnLoadMoreListener(this::loadData, rvList);
        mAdapter.disableLoadMoreIfNotFullPage();*/
    }

    /**
     * todo 这里需要分页
     */
    private void loadData() {
        startTask(CommonBiz.getInstance().shareIndex(), shareIndexBaseResponse -> {
            tvKeTiMoney.setText(shareIndexBaseResponse.data.getKeti_money() + "");
            tvLeiJiMoney.setText(shareIndexBaseResponse.data.getLeiji_money() + "");
            tvUserNum.setText(shareIndexBaseResponse.data.getUser_num() + "");
            mAdapter.setNewData(shareIndexBaseResponse.data.getList());
        });
       /* //todo 假数据start
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add("");
        }
        //todo 假数据end
        mAdapter.loadMoreComplete();
        if (!CommonUtils.isEmpty(datas)) {
            if (mPageIndex == 1) {
                mAdapter.setNewData(datas);
            } else {
                mAdapter.addData(datas);
            }
        }
        if (mPageIndex++ >= 4) {
            mAdapter.loadMoreEnd(false);
        }*/
    }

    @OnClick(R.id.tv_share)
    public void share() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().share(), baseResponse -> {
            share(root, baseResponse.data);
        });
    }

    @OnClick(R.id.tv_withdraw)
    public void withdraw() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().shareTiXianStatus(), baseResponse -> {
            if (2 == baseResponse.code) {
                new MyAlertDialog(this).builder().setMsg(getString(R.string.un_bind_pay_tip)).
                        setPositiveButton("绑定支付宝", view1 -> startActivity(new Intent(this, BindPayActivity.class))).
                        setNegativeButton("", null).show();
            } else if (0 == baseResponse.code) {
                ToastHelper.show(this, baseResponse.msg);
            } else {
                startActivity(WithDrawActivity.newIntent(this, WithDrawActivity.TYPE_TIXIAN_SHARE, baseResponse.data.getMoney() + "", baseResponse.data.getAlipay_name()));
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShareTixianSucEvent(ShareTixianSucEvent shareTixianSucEvent) {
        loadData();
    }
}
